package com.appkubes.mall.admin.application.goods.specific.controller;

import com.appkubes.cloud.config.params.paramValid.ParamValid;
import com.appkubes.common.Response;
import com.appkubes.common.utils.data.string.UUIDUtils;
import com.appkubes.mall.admin.application.base.BaseController;
import com.appkubes.mall.admin.application.goods.goods.entity.vo.GoodsSkuAddVO;
import com.appkubes.mall.admin.application.goods.goods.entity.vo.GoodsSkuModifyVO;
import com.appkubes.mall.admin.application.goods.specific.entity.vo.GoodsSkuSearchVO;
import com.appkubes.mall.admin.application.goods.specific.service.GoodsSpecificSkuService;
import com.appkubes.mall.core.entity.MallGoodsSpecificSku;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = "商品sku规格模块")
@RestController
@RequestMapping("/goods/sku")
public class GoodsSpecficSkuController extends BaseController {

    @Resource
    GoodsSpecificSkuService goodsSpecificSkuService;

    @ApiOperation("获取sku列表")
    @GetMapping("search")
    public Response search(@ParamValid GoodsSkuSearchVO goodsSkuSearchVO) {
        return Response.success(goodsSpecificSkuService.search(goodsSkuSearchVO));
    }

    @ApiOperation("添加")
    @PostMapping("/add")
    public Response add(GoodsSkuAddVO goodsSkuAddVO) {
        MallGoodsSpecificSku entity = new MallGoodsSpecificSku();
        entity.setSkuDims(goodsSkuAddVO.getSkuDims());
        entity.setGoodsKey(goodsSkuAddVO.getGoodsKey());
        entity.setSkuCode(goodsSkuAddVO.getSkuCode());
        MallGoodsSpecificSku isExit = goodsSpecificSkuService.getOneByEntity(entity);
        if (isExit != null) {
            return Response.error("已存在该规格组合，请勿重复添加");
        }

        MallGoodsSpecificSku mallGoodsSpecificSku = new MallGoodsSpecificSku();
        BeanUtils.copyProperties(goodsSkuAddVO, mallGoodsSpecificSku);
        mallGoodsSpecificSku.setSpecificSkuKey(UUIDUtils.getUUID());
        mallGoodsSpecificSku.setAppKey(null);
        goodsSpecificSkuService.save(mallGoodsSpecificSku);

        if (mallGoodsSpecificSku.getIsDefault() == 1) {
            goodsSpecificSkuService.setDefaultFalse(mallGoodsSpecificSku.getGoodsKey(), mallGoodsSpecificSku.getSkuDims(), mallGoodsSpecificSku.getSkuCode());
        }
        return Response.success(mallGoodsSpecificSku);
    }

    @ApiOperation("删除")
    @GetMapping("/del")
    public Response del(String key) {
        return Response.success(goodsSpecificSkuService.getDao().deleteById(key));
    }

    @ApiOperation("获取详情")
    @GetMapping("/get")
    public Response get(String skuCode, String goodsKey, String dims) {
        MallGoodsSpecificSku mallGoodsSpecificSku = new MallGoodsSpecificSku();
        mallGoodsSpecificSku.setSkuCode(skuCode);
        mallGoodsSpecificSku.setGoodsKey(goodsKey);
        mallGoodsSpecificSku.setSkuDims(dims);
        List<MallGoodsSpecificSku> specificSkuList = goodsSpecificSkuService.selectListByEntity(mallGoodsSpecificSku);
        if (specificSkuList.size() == 0) {
            return Response.error("not found");
        }
        return Response.success(specificSkuList.get(0));
    }

    @ApiOperation("修改")
    @PostMapping("/modify")
    public Response modify(GoodsSkuModifyVO goodsSkuModifyVO) {
        MallGoodsSpecificSku mallGoodsSpecificSku = goodsSpecificSkuService.getById(goodsSkuModifyVO.getSpecificSkuKey());
        BeanUtils.copyProperties(goodsSkuModifyVO, mallGoodsSpecificSku);
        goodsSpecificSkuService.updateById(mallGoodsSpecificSku);

        if (mallGoodsSpecificSku.getIsDefault() == (byte) 1) {
            int ret = goodsSpecificSkuService.setDefaultFalse(mallGoodsSpecificSku.getGoodsKey(), mallGoodsSpecificSku.getSkuDims(), mallGoodsSpecificSku.getSkuCode());
            System.out.println(ret);
        }
        return Response.success(mallGoodsSpecificSku);
    }
}
